//引入express框架
const express = require('express');
const pagination = require('mongoose-sex-page');
const admin= express.Router();
const { User } = require('../model/user');
const { Article } = require('../model/article');
const { Comment } = require('../model/comment');
const { Category } = require('../model/Category');
const { Message } = require('../model/message');
//渲染后台首页
admin.get('/index',async (req,res)=>{
  //计算用户
  let user = (await User.find({role: '用户'})).length;
  //计算管理员
  let admin = (await User.find({role: '管理员'})).length;
  //计算文章数量
  let a = await Article.countDocuments();
  //计算评论数量
  let b = await Comment.countDocuments();
  //计算留言
  let c = await Message.countDocuments();
  //计算文章分类
  let category = await Category.countDocuments();
  res.render('admin/index',{a,b,c,user,admin,category});
});
//渲染文章列表
admin.get('/toArticleListPage',async (req,res)=>{
  let page = req.query.page;
  let category = await Category.find();
  //查询所有文章
  let articles = await pagination(Article).find().populate('author').populate('classify').page(page).size(5).display(3).exec();
  articles = JSON.stringify(articles);
  articles = JSON.parse(articles);
  res.render('admin/articleList',{articles,category});
});
//渲染文章分类页面信息
admin.get('/categories',async (req,res)=>{
  let category = await Category.find();
  res.render('admin/categories',{
    category
  });
});
//添加文章分类功能
admin.post('/addCategory',require('./admin/addCategory'));

//渲染修改文章分类页面
admin.get('/toModifyClassifyPage',async (req,res)=>{
  let category = await Category.findOne({_id: req.query.id});
    res.render('admin/modifyCategory',{
      category
    });
});
//修改文章分类功能
admin.post('/modifyCategory',require('./admin/modifyClassify'));

//删除文章分类信息功能
admin.get('/deleteCategory',require('./admin/deleteCategory'));
//用户列表
admin.get('/toUserList',async (req,res)=>{
  let page = req.query.page;
  //查询所有用户
  let users = await pagination(User).find().page(page).size(5).display(3).exec();
  //console.log(users);
  res.render('admin/userList',{
    users
  });
});
//渲染添加文章页面
admin.get('/toAddArticle',async (req,res)=>{
  let category = await Category.find();
  res.render('admin/addArticle',{category});
});
//渲染添加用户页面
admin.get('/toAddUserPage',(req,res)=>{
  res.render('admin/addUser',{});
});
//实现添加用户功能
admin.post('/addUser', require('./admin/addUser'));
//修改用户状态功能
admin.get('/modifyState',require('./admin/modifyUserState'));
//删除用户功能
admin.get('/deleteUser',require('./admin/deleteUser'));
//渲染用户修改页面
admin.get('/toModifyUserPage',async (req,res)=>{
  //根据传入过来的id查询用户信息
  let user = await User.findOne({_id: req.query.id});
  if (user){
    res.render('admin/modifyUser',{
      user
    });
  }else{
    res.render('admin/userList',{
      message: '修改失败！！！'
    });
  }

});
//修改用户信息功能
admin.post('/modifyUser',require('./admin/modifyUser'));
//验证用户名唯一
admin.get('/checkUsername',async (req,res)=>{
  //console.log(req.query.username);
  //根据传入过来的用户名查找
  let user = await User.findOne({username: req.query.username});
  if (user){
    res.send({
      state: 0,
      message: '用户名已被注册！！！'
    });
  }else{
    res.send({
      state: 1,
      message: '恭喜，用户名可以注册。'
    });
  }
})
//添加文章功能
admin.post('/addArticle',require('./admin/addArticle'));
//修改文章状态功能
admin.get('/modifyArticleState',require('./admin/modifyArticleState'));
//删除文章功能
admin.get('/deleteArticle',require('./admin/deleteArticle'));
//渲染文章修改页面
admin.get('/toModifyArticlePage',async (req,res)=>{
  let article = await Article.findOne({_id: req.query.id}).populate('author');
  article = JSON.stringify(article);
  article = JSON.parse(article);
  res.render('admin/modifyArticle',{
    article
  });
});
//修改文章功能
admin.post('/modifyArticle',require('./admin/modifyArticle'));

//渲染评论列表
admin.get('/toCommentListPage',async (req,res)=>{
  let page = req.query.page;
  let comments = await pagination(Comment).find().populate('uid').populate('aid').page(page).size(5).display(3).exec();
  comments = JSON.stringify(comments);
  comments = JSON.parse(comments);
  res.render('admin/commentList',{
    comments
  })
});

//修改评论状态
admin.get('/modifyCommentState',require('./admin/modifyCommentState'));

//删除评论功能
admin.get('/deleteComment',require('./admin/deleteComment'));

//渲染留言列表
admin.get('/message',require('./admin/message'));

//修改留言状态
admin.get('/modifyMessageState',require('./admin/modifyMessageState'));

//删除留言功能
admin.get('/deleteMessage',require('./admin/deleteMessage'));

//筛选文章
admin.post('/getArticle',require('./admin/getArticle'));

//筛选用户
admin.post('/getUser',require('./admin/getUser'));

//筛选评论
admin.post('/getComment',require('./admin/getComment'));

//筛选留言
admin.post('/getMessage',require('./admin/getMessage'));
module.exports = admin;